How to Check for Multiple Values in a Python List: all()
and any()
Often, you need to determine if all or any of a set of values are present in a Python list.
This guide explains how to efficiently perform these checks using the built-in all()
and any()
functions, along with list comprehensions and generator expressions. We'll also briefly cover alternative approaches using sets and for
loops.
Checking if ALL Values are in a List with all()
The all()
function is the most Pythonic and generally the most efficient way to check if all elements of an iterable are True (or, equivalently, if none of them are False). We combine all()
with a generator expression or a list comprehension that performs the membership check (in
).
Using a Generator Expression
my_list = ['one', 'two', 'three', 'four', 'five']
multiple_values = ['one', 'two', 'three']
if all(value in my_list for value in multiple_values):
print('All of the values are in the list') # This will print
else:
print('Not all of the values are in the list')
print(all(value in my_list for value in multiple_values)) # Output: True
value in my_list
: This is the core check. Thein
operator tests for membership (isvalue
present inmy_list
?).for value in multiple_values
: This iterates over each value in themultiple_values
list.(value in my_list for value in multiple_values)
: This is a generator expression. It efficiently produces a sequence of boolean values (True/False) without creating an intermediate list.all(...)
: Theall()
function checks if all of the boolean values produced by the generator expression areTrue
.
Using a List Comprehension
You can use a list comprehension, but it's less memory-efficient:
my_list = ['one', 'two', 'three', 'four', 'five']
multiple_values = ['one', 'two', 'three']
if all([value in my_list for value in multiple_values]): # List comprehension
# ...
- The
[value in my_list for value in multiple_values]
will first create a list with all the boolean results of the check. - The
all()
method will then check if all of the boolean values from the list areTrue
. - This creates an unnecessary intermediate list. The generator expression is generally preferred because it avoids creating this extra list.
Checking if Any Value is in a List with any()
The any()
function is the counterpart to all()
. It returns True
if at least one element of an iterable is True.
my_list = ['one', 'two', 'three']
multiple_values = ['four', 'five', 'three']
if any(item in my_list for item in multiple_values):
print('At least one of the values is in the list') # This will be printed
else:
print('None of the values are in the list')
- The generator expression
(item in my_list for item in multiple_values)
checks eachitem
frommultiple_values
for membership inmy_list
. any(...)
returnsTrue
as soon as it finds any value that's present, andFalse
otherwise.
Getting the Matching Value (if any)
If you need to know which value was found, you can use the walrus operator (:=
) within the generator expression:
my_list = ['one', 'two', 'three']
multiple_values = ['four', 'five', 'three']
if any((match := item) in my_list for item in multiple_values):
print('At least one of the values is in the list')
print(match) # Output: three (the *last* matching value)
else:
print('None of the values are in the list')
(match := item)
: This uses the "walrus operator" (assignment expression) to assign the currentitem
to the variablematch
within the generator expression.- Important:
match
will hold the last value frommultiple_values
that was found inmy_list
. If multiple values match, you only get the last one. If no values match,match
will not be defined.
Alternative: Using Sets (for all()
checks)
For checking if all values are present, you can use set operations. This can be very efficient, especially if multiple_values
is large:
my_list = ['one', 'two', 'three', 'four', 'five']
multiple_values = ['one', 'two', 'three']
if set(multiple_values).issubset(my_list): # Or: set(multiple_values) <= set(my_list)
print('All of the values are in the list') # This will print
else:
print('Not all of the values are in the list')
set(multiple_values)
: Convertsmultiple_values
to a set..issubset(my_list)
: Checks if all elements of the set are present inmy_list
. This is equivalent to the set operation<=
.- This is generally faster than the
all()
approach within
ifmultiple_values
has many elements, because set lookups (in
with a set) are very fast (O(1) on average).
Alternative: Using a for
Loop (Less Efficient)
You can use a for
loop, but it's generally less efficient and less readable than the other methods:
my_list = ['one', 'two', 'three', 'four', 'five']
multiple_values = ['one', 'two', 'three']
multiple_in_list = True # Assume all are present initially
for value in multiple_values:
if value not in my_list:
multiple_in_list = False
break # Exit the loop as soon as one is missing
print(multiple_in_list) # Output: True
- This approach requires explicit looping and a flag variable (
multiple_in_list
). It's more verbose and less efficient than usingall()
or set operations. Avoid this unless you have a very specific reason to do so.